$$.dom = (function(){
	
    var ua = navigator.userAgent.toLowerCase();
    
     $$.isIE     = /msie/.test(ua)
    ,$$.isIE8    = /msie 8/.test(ua)
    ,$$.isGecko  = /gecko/.test(ua)
    ,$$.isFF	 = $$.isGecko;
     
	function cleanEvent_removeCache(n){
        if(Ext.enableNestedListenerRemoval){
        	Ext.EventManager.purgeElement(n, true);
        }else {
            Ext.EventManager.removeAll(n);
        }
		delete Ext.elCache[n.id];
	}
	var delDummy;
	
	return {
		getEl : function(elStr, eventHandler, eventName,isIE_Check){
			// 利用就近原则 判断是否 在当前 $$ 上，若不是，则为 apply/call 的操作。
			var target = this == $$ ? document : this;
			var elObj = target.getElementById(elStr);
			
			if(elObj == null){ 
				var msg = '没有那个{0}元素！'.format(elStr);
				return null;
			}
			
            eventHandler && $$.event.on(
            	elObj, eventHandler.eventName || eventName || 'click', eventHandler
            );
            
            // IE returns elements with the 'name' and 'id' attribute.
            // we do a strict check to return the element with only the id attribute
			return $$.isIE && isIE_Check ? elObj.getAttribute('id') == elStr : elObj;
		}
		,
		removeNode:  function(n){
			if($$.isIE && ! $$.isIE8){
		        if(n && n.tagName != 'BODY'){
		        	cleanEvent_removeCache(n);
		            delDummy = delDummy || document.createElement('div');
		            delDummy.appendChild(n);
		            delDummy.innerHTML = '';
		        }
			}else{
	            if(n && n.parentNode && n.tagName != 'BODY'){
	            	cleanEvent_removeCache(n);
	                n.parentNode.removeChild(n);
	            }
			}
		}
		,scriptLoader : function(url, onLoadFn, onErrorFn){
            var documentHead = document.head 
            				|| document.getElementsByTagName('head')[0];
			var script		 = document.createElement('script');
			
            script.src		= url;
            script.onload	= onLoadFn.delegate(null, url);	// for FF
            script.onerror	= onErrorFn || $$.loadFail;
            script.onreadystatechange = function() {		// for IE
            	var state = this.readyState;
            	
            	$$.log && $$.log('读取位置{0}的内容，请稍候，当前状态：{1}'.format(url, state));
            	
                if (state === 'loaded' || state === 'complete') {
                    onLoadFn(null, url);
                }
            };
            
            documentHead.appendChild(script);
            
            documentHead = script = null;
		}
	};
})();

$$.loadFail = function(){
	l('加载失败！');
}

$$.getEl = $$.dom.getEl; // shorthand

$$.Cookies = {
	/**
	 * 读取Cookie
	 * @param	{String} Name
	 * @return	{String}
	 */
	get : (function(key){
		var arr = document.cookie.match(new RegExp("(?:^| )" + key + "=([^;]*)[;$]"));
		return arr && unescape(arr[1]);
	}).after($$.getPrimitives)
	
	/**
	 * 设置Cookies
	 * if the expires variable is set, make the correct expires time, the current script below will set it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24
	 * @param {String}	name
	 * @param {Mixed}	value 
	 * @param {Number}	expireDays 过期天数
	 * @return value
	 */
	,set : function (key, value, expires, path, domain, secure){
		var cookie = [key + "=" + escape(value)];

		if  (expires){
			 expires = expires * 1000 * 60 * 60 * 24
			,expires = new Date(new Date().getTime() + expires)
			,expires = expires.toGMTString();
			
			cookie.push('expires='	+ expires);
		}

		path	&& cookie.push('path='	 + path);
		domain	&& cookie.push('domain=' + domain);
		secure	&& cookie.push('secure');
		
		document.cookie = cookie.join('; ');
		
		return value;
	}
	
	/**
	 * 删掉 Cookie。
	 * @param {String} key Cookie 之 ID。
	 */
	,del : function(key){
		document.cookie = key + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
	}
	
	/**
	 * 怎样使用Cookie跟踪来访者？
	 * 看看下面的代码，我们用它来持续跟踪一个用户，以获知用户访问过多少次网站页面。
	 * 其中，“edk_visited_user”就是我们cookie的默认标识。
	 * @return {Number} 第几次访问。
	 */
	,visitCounter : function(){
		var key = 'edk_visited_user';
		var i	= this.get(key);
		
		return this.set(key, i ? ++(Number(i)) : 1/* 第一次访问，没有设置过cookies */, 99, '/');
	}
};